import json

from app.service.usr_service import *
from model.tree import *

logger = log.Logger()


def get_base_info(user_id):
    usr_dao = User_DAO()
    usr = usr_dao.get_user_info(user_id)
    return usr

def update_base_info(user_id, user_name, email, phone,cost,rate,risk,time,avator):
    usr_dao = User_DAO()
    usr = usr_dao.update_user_info(user_id=user_id, user_name=user_name,
                                   email=email, phone=phone,cost=cost,rate=rate,risk=risk,time=time,avator=avator)
    return usr


def get_model(id, cost, rate, risk, time):
    result = recommend_portfolio(id, cost, rate, risk, time)
    welcome_msg = result[0]
    amount = result[1]
    expected_profit = result[2]
    low_risk_ratio = result[3]
    high_risk_ratio = result[4]
    maximum_profit = result[5]
    maximum_loss = result[6]

    return json.dumps({'welcome_msg': welcome_msg, 'amount': amount,
                       'expected_profit': expected_profit, 'low_risk_ratio': low_risk_ratio,
                       'high_risk_ratio': high_risk_ratio, 'maximum_profit': maximum_profit,
                       'maximum_loss': maximum_loss})


def do_login(usr_account, usr_passwd):
    usr_dao = User_DAO()
    user_id = usr_dao.do_login(usr_account, usr_passwd)
    return user_id


def do_register(usr: user_entity.User):
    usr_dao = User_DAO()
    user_id = usr_dao.do_register(usr)
    return user_id


# test
if __name__ == "__main__":
    # do_login('webrainer', 'ab630082b15832228db8247a4b020bab42d94ed59ff0586074b86edbae657089')
    get_base_info(14)
